#include "controller.h"

Controller::Controller( const BookRepository &bookRepository,
                        const PersonRepository &personRepository )
: m_bookRepository( bookRepository ),
  m_personRepository( personRepository )
{
	m_personList = personRepository.getList();
	m_bookList = bookRepository.getList();
}

void Controller::updateDataBase()
{
	m_bookRepository.updateList( m_bookList );
	m_personRepository.updateList( m_personList );

	m_bookRepository.updateDataBase();
	m_personRepository.updateDataBase();
}

void Controller::addBook( const Book &book )
{
	qDebug("append()");
	m_bookList.append( book );
}

void Controller::addPerson( const Person &person )
{
	m_personList.append( person );
}

void Controller::removeBook( const Book &book )
{
	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == book.getId() )
		{
			m_bookList.remove( i );
			break;
		}
	}
}

void Controller::removeBook( const ID &id )
{
	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == id )
		{
			m_bookList.remove( i );
			break;
		}
	}
}

void Controller::removePerson( const Person &person )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == person.getId() )
		{
			m_personList.remove( i );
			break;
		}
	}
}

void Controller::removePerson( const ID &id )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == id )
		{
			m_personList.remove( i );
			break;
		}
	}
}

void Controller::borrowBook( const Book &book, const Person &person )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == person.getId() )
		{
			m_personList.value( i ).borrowBook( book.getId() );
			updateDataBase();
			break;
		}
	}

	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == book.getId() )
		{
			m_bookList.value( i ).borrowBook();
			updateDataBase();
			break;
		}
	}
}

void Controller::borrowBook( const ID &bookId, const ID &personId )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == personId )
		{
			m_personList.value( i ).borrowBook( bookId );
			updateDataBase();
			break;
		}
	}

	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == bookId )
		{
			m_bookList.value( i ).borrowBook();
			updateDataBase();
			break;
		}
	}
}

void Controller::returnBook( const Book &book, const Person &person )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == person.getId() )
		{
			m_personList.value( i ).returnBook( book.getId() );
			updateDataBase();
			break;
		}
	}

	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == book.getId() )
		{
			m_bookList.value( i ).returnBook();
			updateDataBase();
			break;
		}
	}
}

void Controller::returnBook( const ID &bookId, const ID &personId )
{
	for( int i = 0; i < m_personList.size(); i++ )
	{
		if( m_personList.value( i ).getId() == personId )
		{
			m_personList.value( i ).returnBook( bookId );
			updateDataBase();
			break;
		}
	}

	for( int i = 0; i < m_bookList.size(); i++ )
	{
		if( m_bookList.value( i ).getId() == bookId )
		{
			m_bookList.value( i ).returnBook();
			updateDataBase();
			break;
		}
	}
}

BookRepository Controller::getBookRepository()
{
	return m_bookRepository;
}

PersonRepository Controller::getPersonRepository()
{
	return m_personRepository;
}
